package org.example.ttms.entityClass;

import javafx.collections.FXCollections;
import org.example.ttms.entityClass.ticket.Ticket;
import org.example.ttms.entityClass.train.Route;
import org.example.ttms.entityClass.train.Trips;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Listening {
    public static ScheduledExecutorService scheduler;

    public void start(){
        scheduler = Executors.newScheduledThreadPool(1);
        listening();
    }

    public void listening(){
        Runnable task = () -> {
            Date date = new Date();
            listeningTrips(date);
            listeningTicket(date);
        };
        scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES);
    }

    public void end(){
        scheduler.shutdown();
    }

    public void listeningTrips(Date date){
        Trips trips = new Trips();
        List<Trips> tripsList = trips.obtainAllTrips();
        for (Trips tmp : tripsList) {
            if (!tmp.getStatus().equals("出发") && !tmp.getStatus().equals("到达")) {
                Route route = new Route();
                HashMap<Integer, Route> hashMap = route.obtainAllRoute();
                int time = tmp.obtainTime(hashMap.get(tmp.getRouteId()), 1);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(tmp.getGoTime());
                calendar.add(Calendar.MINUTE, time);
                //出发时间
                Date date2 = calendar.getTime();
                String[] strArr = tmp.getSpeedTime().split("小时|分钟");
                calendar.add(Calendar.MINUTE, Integer.parseInt(strArr[0]) * 60 +
                        Integer.parseInt(strArr[1]));
                //到达时间
                Date date3 = calendar.getTime();
                if (date3.before(date)) {
                    tmp.setStatus("到达");
                    System.out.println(tmp.getTripsId()+"到达");
                } else if (date2.before(date)) {
                    System.out.println(date2);
                    System.out.println(date);
                    System.out.println(tmp.getTripsId()+"出发");
                    tmp.setStatus("出发");
                }
            }
        }
        trips.storageTrips(FXCollections.observableArrayList(tripsList));
    }
    public void listeningTicket(Date date){
        Ticket ticket = new Ticket();
        HashMap<String, Ticket> hashMap = ticket.obtainAllTicket();
        for(Map.Entry<String, Ticket> map : hashMap.entrySet()){
            Ticket tmp = map.getValue();
            if(!tmp.getStatus().equals("已到达") && !tmp.getStatus().equals("未售出")
            && !tmp.getStatus().equals("已退票")){
                if(tmp.getArrivalTime().before(date)){
                    System.out.println(tmp.getTicketId()+"已到达");
                    map.getValue().setStatus("已到达");
                }
                else if(tmp.getGoTime().before(date)){
                    System.out.println(tmp.getTicketId()+"已出发");
                    map.getValue().setStatus("已出发");
                }
            }
        }
        ticket.storageTicket(FXCollections.observableArrayList(hashMap.values()));
    }
}
